<?php

class ListProjectController extends Zend_Controller_Action
{	
    public function init()
    {
        /* Initialize action controller here */
        $this->view->logged = Zend_Auth::getInstance()->hasIdentity();
        
        if (!Zend_Auth::getInstance()->hasIdentity()) {
			$this->_redirect('index/index');
		}
		
		/// A Décommenter si on veut utiliser la redirection sur SSL
		//Perso_ConfigSSL::config();
    }

    public function indexAction()
    {	
        require_once APPLICATION_PATH . '/models/DbTable/Project.php';
		require_once APPLICATION_PATH . '/models/DbTable/Person.php';
		require_once APPLICATION_PATH . '/controllers/IndexController.php';

        $storage = new Zend_Auth_Storage_Session();
		$data = $storage->read();
        $username = $data->username;
        
        $this->view->username = $username;
        
        $Person = new Person();
        $idPerson = $Person->getUserNameId($username);
	
        $this->view->title = "My projects";
        $this->view->headTitle($this->view->title, 'PREPEND');
        $Project= new Project();
      	$this->view->Projects = $Project->getProjects($idPerson);
		
		// Load listproject css
		$baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
		$this->view->headLink()->prependStylesheet($baseUrl.'/css/listproject.css');
    }

    public function addprojectAction()
    {
	    $this->view->idView = "addProjectView";	
    	
		require_once APPLICATION_PATH . '/models/DbTable/Project.php';
		require_once APPLICATION_PATH . '/models/DbTable/Person.php';
		require_once APPLICATION_PATH . '/forms/CreateprojectForm.php';
                    
		$storage = new Zend_Auth_Storage_Session();
		$data = $storage->read();
        $username = $data->username;
        $Person = new Person();
        $idPerson = $Person->getUserNameId($username);
                        
		$Project = new Project();
		$form = new CreateprojectForm();
		
		$this->view->form=$form;
		
		if ($this->getRequest()->isPost()) {
			if ($form->isValid($_POST)) {
				$data = $form->getValues();
				$authAdapter = new Zend_Auth_Adapter_DbTable($Project->getAdapter(),'Project');
				$authAdapter->setIdentityColumn('idProject');
				$data['Person_idPerson']= $idPerson;
		    
				$authAdapter->setIdentity($data['Person_idPerson']);
				
				if ($Project->checkUnique($data['idProject'])) {
					$this->view->errorMessage ="id Project already exist. Please choose  another one.";
					return;
				}
				
				$data['name'] = addslashes($data['name']);
				$data['enterprise'] = addslashes($data['enterprise']);
				$data['website'] = addslashes($data['website']);
				$data['startDate'] = addslashes($data['startDate']);
				$data['endDate'] = addslashes($data['endDate']);
				$data['note'] = addslashes($data['note']);

				$Project->insert($data);
				$this->_redirect('listproject/index');
			}
		}
		
		// Load form css
		$baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
		$this->view->headLink()->prependStylesheet($baseUrl.'/css/form.css');
	}
    
    public function editusersettingsAction()
    {
    	// Hide the login sidebar
   		$this->view->idView = 'editUserSettingsView';
   		
				
		require_once APPLICATION_PATH . '/forms/EditUserSettingsForm.php';
		require_once APPLICATION_PATH . '/models/DbTable/Person.php';
		
		// Retrieve the userid
		$storage = new Zend_Auth_Storage_Session();
		$data = $storage->read();
        $idPerson = $data->idPerson;
        $username = $data->username;
		$this->view->username = $username;
		
		$Person = new Person();
		$form = new EditUserSettingsForm();
		
		// Retrieve the registration form
		$this->view->form = $form;

		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {
				$data = $form->getValues();
				
				$data['name'] = htmlentities($data['name']);
				$data['email'] = htmlentities($data['email']);
				$data['tel'] = htmlentities($data['tel']);
				$data['oldPassword'] = htmlentities($data['oldPassword']);
				$data['password'] = htmlentities($data['password']);
				$data['confirmPassword'] = htmlentities($data['confirmPassword']);
				
				$hash = $Person->getPersonSettings($idPerson);
				$hash = $hash['password'];
				
				if ($data['oldPassword'] != "") {
					if ($hash == hash('sha256',$data['oldPassword'])) {
						$Person->setUserPassword($username, $data['password']);			
					} else {
						$this->view->error = "Wrong old password";
					}
				}
				$Person->setUserSettings($idPerson, $data['name'], $data['email'], $data['tel']);				
				$this->_redirect('listproject/index');
			} else {
				$form->populate($formData);
			}

		} else {
			$DBdata = $Person->getPersonSettings($idPerson);
			$form->populate($DBdata);
		}
		
		// Load form css
		$baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
		$this->view->headLink()->prependStylesheet($baseUrl.'/css/form.css');
    }

    public function deleteprojectAction()
    {
		require_once APPLICATION_PATH . '/models/DbTable/Project.php';
		$this->view->title = "Delete Project";
		$this->view->headTitle($this->view->title, 'PREPEND');
        
        if ($this->getRequest()->isPost()) {
            $del = $this->getRequest()->getPost('del');
            $idCheckbox = $this->getRequest()->getPost('cgu');
            
            $idProject = $this->getRequest()->getPost('idProject');
            if ($del == 'Yes'  ) { 
                $Project = new Project();
				$Project->deleteProject($idProject);	
            }
            $this->_redirect('listproject/index');
        } else {
            $idProject = $this->_getParam('idProject', 0);
            $Project = new Project();
            $this->view->Project = $Project->getProject($idProject);
		} // action body
		
		// Load form css
		$baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
		$this->view->headLink()->prependStylesheet($baseUrl.'/css/form.css');
	}
}